package dk.hojlind.hackhunt.control;

import dk.hojlind.hackhunt.model.ClientSocket;

public class RegistryHandler extends Handler {

	//Public variables

	
	//Private variables
	
	//errors
	String onFalse = "error: User was not created, please try again. If problem persists contact crew";
	String dataInvalid = "error: One or more of your fields is either empty, more than 50 characters or contains illegal characters (;)";
	String error = "error: Something went wrong";
	
	//Constructor
	public RegistryHandler(String adress, int port)
	{
		super(adress, port);
		error = "error";
	}

	
	//Send the user info to the server via socket
	public String addUser(String name, String nick, String pass, String team, String isEvil)
	{
		String[] info = {name, nick, pass, team, isEvil};

		//Check if any of the fields is too short, long or contains unwanted characters
		for(int i = 0; i < info.length; i++)
		{
			if(check(info[i]) == false)
			{
				return dataInvalid;
			}
		}

		String message = "addUser" + ";" + name + ";" + nick + ";" + isEvil + ";" + team + ";" + pass; //testede lige om jeg fik svar med denne endelse, i did not.
		socket = new ClientSocket(adress, port);
		result = socket.processRequest(message);
		
		if(result.contains("false"))
		{
			return onFalse;
		}
		
		return result;
	}
	
	
	public String deleteUser(String nick, String pass)
	{
		String[] info = {nick, pass};

		//Check if any of the fields is too short, long or contains unwanted characters
		for(int i = 0; i < info.length; i++)
		{
			if(check(info[i]) == false)
			{
				return dataInvalid;
			}
		}

		String message = "deleteUser" + ";" + nick + ";" + pass;
		socket = new ClientSocket(adress, port);
		result = socket.processRequest(message);
		
		if(result.contains("false"))
		{
			return onFalse;
		}
				
		return result;
	}

}
